package ru.tapublog.lib.samsungwallet.impl.converter;

import java.util.HashSet;
import java.util.Set;

import ru.tapublog.lib.samsungwallet.api.model.AbstractAlert;
import ru.tapublog.lib.samsungwallet.api.model.AbstractViewElement;
import ru.tapublog.lib.samsungwallet.api.model.Partner;
import ru.tapublog.lib.samsungwallet.api.model.Ticket;
import ru.tapublog.lib.samsungwallet.impl.dao.AlertDAO;
import ru.tapublog.lib.samsungwallet.impl.dao.PartnerDAO;
import ru.tapublog.lib.samsungwallet.impl.dao.TicketDAO;
import ru.tapublog.lib.samsungwallet.impl.dao.TicketDAO.Head;
import ru.tapublog.lib.samsungwallet.impl.dao.ViewElement;

public class TicketConverter {

	public static TicketDAO convertTicket(Ticket ticket) {
		TicketDAO ticketDAO = new TicketDAO();
		Head head = new Head();
		head.setKeywords(ticket.getKeywords());
		head.setSerial(ticket.getSerial());
		head.setSkinId(ticket.getSkinId());
		head.setStorable(ticket.isStoreOnServerEnabled());
		head.setValidUntil(DateConverter.convertDateType(ticket.getValidUntil()));
		head.setVersion(ticket.getVersion());
		ticketDAO.setHead(head);

		if (ticket.getAlerts() != null) {
			Set<AlertDAO> alerts = new HashSet<AlertDAO>();
			for (AbstractAlert alert : ticket.getAlerts()) {
				alerts.add(AlertConverter.convert(alert));
			}
			ticketDAO.setAlerts(alerts);
		}
		
		if (ticket.getPartners()!= null) {
			Set<PartnerDAO> partners = new HashSet<PartnerDAO>();
			for (Partner partner : ticket.getPartners()) {
				partners.add(PartnerConverter.convert(partner));
			}
			ticketDAO.setPartners(partners);
		}
	
		if (ticket.getViewElements()!= null) {
			Set<ViewElement> viewElements = new HashSet<ViewElement>();
			for (AbstractViewElement viewElement : ticket.getViewElements()) {
				viewElements.add(ViewElementConverter.convert(viewElement));
			}
			ticketDAO.setViewElements(viewElements);
		}
		
		return ticketDAO;
	}
}
